package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.annotation.SignatureValidation;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysParise;
import com.ruoyi.project.system.service.ISysPariseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 平台瞬间点赞Controller
 *
 * @author jjp
 * @date 2020-04-14
 */
@RestController
@Api(description = "用户精彩瞬间点赞")
@RequestMapping("/mobileUser/parise")
public class UserPariseController extends BaseController {
    @Autowired
    private ISysPariseService sysPariseService;

    /**
     * 查询平台瞬间点赞列表
     */
    @SignatureValidation
    @ApiOperation("查询是否点赞")
    @GetMapping("/list")
    public AjaxResult list(SysParise sysParise) {
        SysParise parise = new SysParise();
        parise.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        List<SysParise> list = sysPariseService.selectSysParise(parise);
        List<SysParise> momentCommentList = new ArrayList<>();
        List<SysParise> momentList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (null != list.get(i).getMomentId() && list.get(i).getMomentId().equals(sysParise.getMomentId())) {
                momentList.add(list.get(i));
            } else if (null != list.get(i).getMomentCommentId()) {
                momentCommentList.add(list.get(i));
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("momentList", momentList);
        map.put("momentCommentList", momentCommentList);
        return AjaxResult.success(map);
    }


    /**
     * 新增平台瞬间点赞
     */
    @RepeatSubmit
    @SignatureValidation
    @ApiOperation("新增平台瞬间点赞和评论点赞")
    @Log(title = "平台瞬间点赞", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysParise sysParise) {
        sysParise.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        List<SysParise> sysParises = sysPariseService.selectSysParise(sysParise);
        if (sysParises.size() == 0) {
            sysPariseService.insertSysParise(sysParise);
            Integer count = sysPariseService.selectCount(sysParise);
            return AjaxResult.success(count);
        }
        return AjaxResult.error("您已点过赞！");

    }

    /**
     * 删除平台瞬间点赞
     */
    @SignatureValidation
    @ApiOperation("取消瞬间点赞")
    @Log(title = "平台瞬间点赞", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    public AjaxResult remove(@RequestBody SysParise sysParise) {
        Long mobileUserId = SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId();
        sysParise.setMobileUserId(mobileUserId);
        sysPariseService.deleteSysPariseByMobileUserId(sysParise);
        Integer count = sysPariseService.selectCount(sysParise);
        return AjaxResult.success(count);
    }

}
